package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.BeerView;

public class BeerViewPresenter extends AbstractPresenter<BeerView> {
	final String SINGLEITEM = "beerView";
	final String MULTIITEM = "beerViews";
	
	public ArrayList<BeerView> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public BeerView tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public BeerView createItemFromJSONObject(JSONObject jsonObject){
		BeerView item = new BeerView();
		try{
			item.setBeerCOVols(jsonObject.getDouble("BCOVols"));
			item.setInProduction(jsonObject.getBoolean("inProduction"));
			item.setRecipeID(jsonObject.getInt("recipeID"));
			item.setBeerABV(jsonObject.getDouble("bAbv"));
			item.setBeerIBU(jsonObject.getDouble("bIbu"));
			item.setBeerID(jsonObject.getInt("bId"));
			item.setBeerName(jsonObject.getString("bName"));
			item.setBeerSRM(jsonObject.getDouble("bSrm"));
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(BeerView item){
		try{
			JSONObject json = new JSONObject();
			json.put("BCOVols", item.getBeerCOVols());
			json.put("inProduction", item.getInProduction());
			json.put("recipeID", item.getRecipeID());
			json.put("bAbv", item.getBeerABV());
			json.put("bIbu", item.getBeerIBU());
			json.put("bId", item.getBeerID());
			json.put("bName", item.getBeerName());
			json.put("bSrm", item.getBeerSRM());
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public BeerView getEmptyEntity(){
		BeerView entity = new BeerView();
		return entity;
	}
}
